/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Interface.java
 *
 * Created on 13/04/2011, 17:41:58
 */
package matriz;

import java.awt.Color;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;
import java.util.ArrayList;
import java.util.regex.*;
import javax.swing.JOptionPane;

/**
 *
 * @author lukete
 */
public class Interface extends javax.swing.JFrame {

    private int x, y, xL = 1, yC = 1;
    JFormattedTextField[][] jfCampos;
    Matriz mat = new Matriz();
    MaskFormatter numero;
    ArrayList<JFormattedTextField> determinante;

    /** Creates new form Interface */
    public Interface() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    void defineGraficoXY() {
        x = Integer.parseInt(jcbOrdem.getSelectedItem().toString());
        mat.setX(x);
        y = x + 1;
        mat.setY(y);
        System.out.println("X:" + x + " Y: " + y);
    }

    void geraArrayListJFormattedTextField() {
        determinante = new ArrayList<JFormattedTextField>();
        for (int i = 0; i < y; i++) {
            determinante.add(new JFormattedTextField());
        }
    }

    void geraArrayJFormattedTextField() {
        jfCampos = new JFormattedTextField[x][y];
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                jfCampos[i][j] = new JFormattedTextField();
            }
        }
    }

    void setJFormattedTextField(int i, int j, int xL, int yC, JFormattedTextField[][] jfCamposParam, JPanel painel) {
        jfCamposParam[i][j].setBounds(xL, yC, 0, 0);
        jfCamposParam[i][j].setSize(44, 20);
        jfCamposParam[i][j].setVisible(true);
        painel.add(jfCamposParam[i][j]);
        painel.repaint();

    }

    void setJTextFormattedList(int l, int yC, ArrayList<JFormattedTextField> determinanteParam, JPanel painel) {
        determinanteParam.get(l).setBounds(0, yC, 0, 0);
        determinanteParam.get(l).setSize(55, 20);
        determinanteParam.get(l).setVisible(true);
        determinanteParam.get(l).setEnabled(false);
        painel.add(determinanteParam.get(l));
        painel.repaint();
    }

    void exibeJFormattedTextField() {
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                setJFormattedTextField(i, j, xL, yC, jfCampos, jPanelMatrizCompleta);
                xL += 60;
            }
            yC += 40;
            xL = 1;
        }
        yC = 1;
        for (int l = 0; l < y; l++) {
            setJTextFormattedList(l, yC, determinante, jPanelResultados);
            yC += 40;
        }
    }
//matches("^-?[0-9]+$"))
    void validaJFormattedTextField() {
        boolean contValidate=true;
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                if (!jfCampos[i][j].getText().toString().matches("[-]?[0-9]+")) {
                    jfCampos[i][j].setBackground(Color.red);
                    contValidate=false;
                } else {
                    jfCampos[i][j].setBackground(Color.white);
                }

            }
        }
        if (contValidate) {
            mat.setMatriz();
            mat.criarMatrizJTextFieldFormatted(jfCampos);
            mat.clonaMatriz();
            mat.resolveCramer();
            mat.determinantesDeCramer();
            mat.exibeDeterminantesDeCramer();
            mat.armazenaDeterminanteCramer.add(mat.armazenaDeterminanteCramer.get(0));
            mat.armazenaDeterminanteCramer.remove(0);
            mat.exibeDeterminantesDeCramerJFormattedTextField(determinante);
            System.out.print("Valores de todas det =>"+mat.armazenaDeterminanteCramer+"\n Um por um=>"+mat.armazenaCramer+"\n");
            System.out.println("Valot TF=> "+determinante.get(0).getText()+" "+determinante.get(1).getText()+" "+determinante.get(2).getText()); 
            
            //            mat.armazenaDeterminanteCramer.clear();
//            mat.armazenaCramer.clear();

        } else {
//            System.out.println("Fora do for=>" +contValidate);
            JOptionPane.showMessageDialog(jbtnGerar, "Campos em vermelho sao obrigatorios", "Alerta!!", JOptionPane.INFORMATION_MESSAGE);
        }
       

    }

    void criaMascara() {
        try {
            numero = new MaskFormatter("*#");
        } catch (java.text.ParseException e) {
//            System.out.print("ERRO");
        }

    }

    void zeraBoundsJFormattedTextField(int zero) {
        this.xL = zero;
        this.yC = zero;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jcbOrdem = new javax.swing.JComboBox();
        jlblOrdem = new javax.swing.JLabel();
        jbtnGerar = new javax.swing.JButton();
        jPanelMatrizCompleta = new javax.swing.JPanel();
        jbtnResult = new javax.swing.JButton();
        jPanelResultados = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Sistema Linear");
        setAlwaysOnTop(true);
        setBackground(new java.awt.Color(204, 204, 204));
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setFont(new java.awt.Font("Comic Sans MS", 0, 10));
        setModalExclusionType(java.awt.Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
        setName("jFramePrincipal"); // NOI18N

        jcbOrdem.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2", "3", "4" }));
        jcbOrdem.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcbOrdemItemStateChanged(evt);
            }
        });

        jlblOrdem.setText("Escolha a ordem da matriz:");

        jbtnGerar.setText("Gerar Matriz");
        jbtnGerar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnGerarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanelMatrizCompletaLayout = new org.jdesktop.layout.GroupLayout(jPanelMatrizCompleta);
        jPanelMatrizCompleta.setLayout(jPanelMatrizCompletaLayout);
        jPanelMatrizCompletaLayout.setHorizontalGroup(
            jPanelMatrizCompletaLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 298, Short.MAX_VALUE)
        );
        jPanelMatrizCompletaLayout.setVerticalGroup(
            jPanelMatrizCompletaLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 225, Short.MAX_VALUE)
        );

        jbtnResult.setText("Determinante");
        jbtnResult.setEnabled(false);
        jbtnResult.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnResultActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanelResultadosLayout = new org.jdesktop.layout.GroupLayout(jPanelResultados);
        jPanelResultados.setLayout(jPanelResultadosLayout);
        jPanelResultadosLayout.setHorizontalGroup(
            jPanelResultadosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 84, Short.MAX_VALUE)
        );
        jPanelResultadosLayout.setVerticalGroup(
            jPanelResultadosLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 306, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jlblOrdem)
                    .add(layout.createSequentialGroup()
                        .add(47, 47, 47)
                        .add(jcbOrdem, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(27, 27, 27)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jbtnGerar)
                            .add(jbtnResult))))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanelMatrizCompleta, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(77, 77, 77)
                .add(jPanelResultados, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(39, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jlblOrdem)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jcbOrdem, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(jbtnGerar)
                        .add(11, 11, 11)
                        .add(jbtnResult))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanelMatrizCompleta, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jPanelResultados, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(56, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jcbOrdemItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcbOrdemItemStateChanged
        jbtnGerar.setEnabled(true);
        jbtnResult.setEnabled(false);
        jPanelMatrizCompleta.removeAll();
        jPanelResultados.removeAll();
        jPanelMatrizCompleta.repaint();
        jPanelResultados.repaint();

    }//GEN-LAST:event_jcbOrdemItemStateChanged

    private void jbtnResultActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnResultActionPerformed
        validaJFormattedTextField();
    }//GEN-LAST:event_jbtnResultActionPerformed

    private void jbtnGerarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnGerarActionPerformed
           defineGraficoXY();
//        =========================================================================
        

//        =========================================================================
        criaMascara();
        geraArrayJFormattedTextField();
        geraArrayListJFormattedTextField();
        exibeJFormattedTextField();
        zeraBoundsJFormattedTextField(0);
        jbtnGerar.setEnabled(false);
        jbtnResult.setEnabled(true);
        mat.armazenaCramer.clear();
        mat.armazenaDeterminanteCramer.clear();
        System.out.print("\nValores de todas det =>"+mat.armazenaDeterminanteCramer+"\n Um por um=>"+mat.armazenaCramer+"\n");
        
    }//GEN-LAST:event_jbtnGerarActionPerformed
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Interface().setVisible(true);
                Interface interf = new Interface();

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanelMatrizCompleta;
    private javax.swing.JPanel jPanelResultados;
    private javax.swing.JButton jbtnGerar;
    private javax.swing.JButton jbtnResult;
    private javax.swing.JComboBox jcbOrdem;
    private javax.swing.JLabel jlblOrdem;
    // End of variables declaration//GEN-END:variables
}
